using System;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Auto.Data.HumanResource;
using Auto.Web.Common;

namespace BaseSystem._System.UserControl
{
    public partial class ucEmpBasic : System.Web.UI.UserControl
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                ClearTextBox();
            }

        }
        public void List(string diviID)
        {
            EmpBasic emp = new EmpBasic();
            GridViewEx1.DataSource = emp.GetDataByKey("", diviID);
            GridViewEx1.DataBind();
        }

        protected void GridViewEx1_RowCommand(object sender, GridViewCommandEventArgs e)
        {
            ClearTextBox();
            switch(e.CommandName)
            {
                case "Selected":
                    int rowIndex=Convert.ToInt32(e.CommandArgument);
                    Label lblGVEmpID = GridViewEx1.Rows[rowIndex].FindControl("lblGVEmpID") as Label;
                    View(lblGVEmpID.Text);
                    txtEmpID.ReadOnly = true;
                    btnAdd.Enabled = false;
                    btnDelete.Enabled = true;
                    btnUpdate.Enabled = true;
                    break;
                case "Add": 
                    txtEmpID.ReadOnly = false;
                    btnAdd.Enabled = true;
                    btnUpdate.Enabled = false;
                    btnDelete.Enabled = false;
                    break;
            }
        }
        private void ClearTextBox()
        {
            txtCardNO.Text = "";
            //UcDivision1.DivID = "";
            txtEmail.Text = "";
            txtLevelNO.Text = "";
            ddlMarried.Text = "";
            txtName.Text = "";
            txtNote.Text = "";
            txtOfficePhoneExt.Text = "";
            txtEmpID.Text = "";
            UcDateTextBox1.ClearDate();
            ddlSex.Text = "";
        }
        private void View(string empID)
        {
            EmpBasic emp = new EmpBasic(empID);
            txtPersonID.Text = emp.PersonID;
            if (!string.IsNullOrEmpty(emp.BirthDay))
            {
                UcDateTextBox1.CalendarDate = Convert.ToDateTime(emp.BirthDay);
            }
            txtCardNO.Text = emp.CardNO;
            UcDivision1.DivID = emp.DivID;
            txtEmail.Text = emp.Email;
            txtLevelNO.Text = emp.LevelNO;
            ddlMarried.Text = emp.Married.ToString().ToLower();
            txtName.Text = emp.Name;
            txtNote.Text = emp.Note;
            txtOfficePhoneExt.Text = emp.OfficePhoneExt;
            txtEmpID.Text = emp.EmpID;
            ddlSex.Text = emp.sex.ToString().ToLower();
        }

        protected void btnAdd_Click(object sender, EventArgs e)
        {
            Save();
        }

        protected void btnUpdate_Click(object sender, EventArgs e)
        {
            Update();
        }

        protected void btnDelete_Click(object sender, EventArgs e)
        {

        }
        private EmpBasic GetEmpBasic()
        {
            EmpBasic emp = new EmpBasic(txtEmpID.Text);
            emp.PersonID = txtPersonID.Text;
            emp.ActionEmpID = SessionBox.User.EmpID;
            emp.BirthDay = UcDateTextBox1.CalendarDate.ToShortDateString();
            emp.CardNO = txtCardNO.Text;
            emp.DivID = UcDivision1.DivID;
            emp.Email = txtEmail.Text;
            emp.EmpID = txtEmpID.Text;
            emp.LastUpdateDate = DateTime.Now;
            emp.LastUpdateIP = Request.ServerVariables["Remote_Addr"];
            emp.LevelNO = txtLevelNO.Text;
            emp.Married = Convert.ToBoolean(ddlMarried.SelectedValue);
            emp.Name = txtName.Text;
            emp.Note = txtNote.Text;
            emp.OfficePhoneExt = txtOfficePhoneExt.Text;
            emp.sex = Convert.ToBoolean(ddlSex.SelectedValue);
            return emp;
        }
        private void Update()
        {
            EmpBasic emp = GetEmpBasic();
            emp.Update();

        }
        private void Save()
        {
            EmpBasic emp = GetEmpBasic();
            if (!emp.Exists(emp.EmpID))
            {
                emp.Insert();
            }
        }
        public string SetDivID
        {
            set { UcDivision1.DivID = value; }
        }
    }
}